package org.stephen.recommend.util;

import org.apache.poi.ss.formula.functions.T;

import java.util.*;

/**
 * ClassName: CommonUtils
 * Description:
 * Date: 2020/12/9 14:30
 *
 * @author stephen.qiu
 */
public class CommonUtils {
    /**
     * 把多个List转换成一个Set
     *
     * @param arrays
     * @return
     */
    public static Set<String> getSetByLists(List... arrays) {
        Set<String> set = new HashSet<>();
        for (List arr : arrays) {
            if (arr != null) {
                set.addAll(arr);
            }
        }
        if (set.size() == 0) {
            return null;
        }
        return set;
    }

    /**
     * 初始化HashMap的大小，防止过程中扩容次数太多
     *
     * @param size 预估HashMap大小
     * @return
     */
    public static int initHashMapSize(int size) {
        return size <= 0 ? 16 : (int) Math.pow((int) CommonUtils.log2(size) + 1, 2);
    }

    /**
     * Math.log的底为e
     *
     * @param n log2(n)
     * @return
     */
    private static double log2(double n) {
        return Math.log(n) / Math.log(2);
    }

    /**
     * 返回升序比较器
     *
     * @return
     */
    public static Comparator getComparator() {
        Comparator valueComparator = new Comparator<Map.Entry<T, Double>>() {
            @Override
            public int compare(Map.Entry<T, Double> o1, Map.Entry<T, Double> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        };
        return valueComparator;
    }
}
